package com.longy.gulimall.product.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.longy.gulimall.product.entity.CategoryEntity;
import com.longy.gulimall.product.service.CategoryService;
import com.longy.common.utils.R;

import javax.xml.crypto.Data;


/**
 * 商品三级分类
 *
 * @author longyang
 * @email longyang3240@163.com
 * @date 2020-11-14 16:27:07
 */
@RestController
@RequestMapping("product/category")
@Transactional
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类，并以树形结构显示列表
     */
    @RequestMapping("/list/tree")
    public R listTree(){

        List<CategoryEntity> listTree = categoryService.listWithTree();

        return R.ok().put("data", listTree);
    }


    /**
     * 查询所有分类，并以树形结构显示列表
     */
    @RequestMapping("/list1")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public R list(){
        long star1 = System.currentTimeMillis();
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","图书、音像、电子书刊");
        List<CategoryEntity> listTree = categoryService.list(queryWrapper);
        categoryService.update(listTree.get(0));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<CategoryEntity> newlist = categoryService.list(queryWrapper);
        Date date = new Date();
        System.out.println("时间"+date+"线程："+Thread.currentThread() + "==提交事物");
        return R.ok().put("data", newlist);
    }
    @RequestMapping("/list2")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public R list2(){
        long star1 = System.currentTimeMillis();
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","图书、音像、电子书刊");
        List<CategoryEntity> listTree = categoryService.list(queryWrapper);
        categoryService.update(listTree.get(0));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<CategoryEntity> newlist = categoryService.list(queryWrapper);
        return R.ok().put("data", newlist);
    }
    @RequestMapping("/list3")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public R list3(){
        long star1 = System.currentTimeMillis();
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","图书、音像、电子书刊");
        List<CategoryEntity> listTree = categoryService.list(queryWrapper);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        categoryService.update(listTree.get(0));
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        List<CategoryEntity> newlist = categoryService.list(queryWrapper);
        return R.ok().put("data", newlist);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }


    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetail(category);

        return R.ok();
    }

    /**
     * 删除
     * @RequestBody获取请求体的内容
     * 自动转换为
     *
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
